package cn.ycxy.lbl.posts.controller;


import cn.ycxy.lbl.common.restful.R;
import cn.ycxy.lbl.pojo.posts.dto.CommentAddDTO;
import cn.ycxy.lbl.posts.service.ICommentService;
import cn.ycxy.lbl.posts.utils.SecurityContextUtils;
import cn.ycxy.lbl.posts.utils.SensitiveFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "帖子评论模块", value = "帖子评论模块")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    @ApiOperation("添加帖子评论回复")
    @PostMapping("/add")
    @PreAuthorize("hasRole('user')")
    public R<Void> addComment(@Validated @RequestBody CommentAddDTO commentAddDTO) {
        //获取登录用户id
        Long userId = SecurityContextUtils.getUserId();
        commentAddDTO.setUserId(userId.intValue());
        //评论过滤
        commentAddDTO.setContent(sensitiveFilter.filter(commentAddDTO.getContent()));
        commentService.addComment(commentAddDTO);
        return R.ok();
    }

}
